package cn.wolfcode.luowowo.service.impl;

import cn.wolfcode.luowowo.domain.StrategyComment;
import cn.wolfcode.luowowo.query.StrategyCommentQuery;
import cn.wolfcode.luowowo.repository.StrategyCommentRepository;
import cn.wolfcode.luowowo.service.IStrategyCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class StrategyCommentServiceImpl implements IStrategyCommentService {


    @Autowired
    private StrategyCommentRepository strategyCommentRepository;


    @Autowired
    private MongoTemplate mongoTemplate;


    @Override
    public void save(StrategyComment strategyComment) {
        // 因为Id和userInfo的Id同名 也会被覆盖  需要给他设个null值  让芒果DB 自动生成
        strategyComment.setId(null);
        strategyCommentRepository.save(strategyComment);
    }

    @Override
    public void update(StrategyComment strategyComment) {

        strategyCommentRepository.save(strategyComment);
    }

    @Override
    public void delete(String id) {
        strategyCommentRepository.deleteById(id);

    }

    @Override
    public StrategyComment get(String id) {
        Optional<StrategyComment> op = strategyCommentRepository.findById(id);
        if (op.isPresent()){
            StrategyComment strategyComment = op.get();
            return strategyComment;
        }
        return null;
    }

    @Override
    public List<StrategyComment> list()
    {
        return strategyCommentRepository.findAll();
    }

    // 分页操作
    @Override
    public Page<StrategyComment> query(StrategyCommentQuery qo) {

        Query query = new Query();
        // 查询总页数
        long count = mongoTemplate.count(query, StrategyComment.class);
        if (count == 0){
            return Page.empty();
        }

        /**
         * 根据攻略明细分类
         */
        query.addCriteria(Criteria.where("strategyId").is(qo.getStrategyId()));

        // 分页排序
        Pageable pageable = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(), Sort.Direction.ASC, "_id");

        query.with(pageable);

        List<StrategyComment> list = mongoTemplate.find(query, StrategyComment.class);


        return new PageImpl<>(list,pageable,count);

    }

    /**
     * 第一次点赞和第二次点赞
     * @param cid
     * @param id
     */
    @Override
    public void commentThumb(String cid, String id) {
        // 通过传上来的攻略评论Id 找到董略评论对象
        StrategyComment strategyComment = this.get(cid);
        // 拿到攻略评论对象的Thumbuplist 属性
        List<String> thumbups = strategyComment.getThumbuplist();
        // 如果不包含当前用户的Id
        if (!(thumbups.contains(id))){
            // 点赞数 + 1
            strategyComment.setThumbupnum(strategyComment.getThumbupnum() + 1 );
            // 把当前用户的Id 加到 评论Id List的属性中
            thumbups.add(id);
            strategyComment.setThumbuplist(thumbups);
        }else{
            // 如果包含当前用户的Id
            // 点赞数 - 1
            strategyComment.setThumbupnum(strategyComment.getThumbupnum() - 1 );
            // 删除当前用户的Id
            thumbups.remove(id);
            strategyComment.setThumbuplist(thumbups);
        }
        // 执行修改方法
        mongoTemplate.save(strategyComment);

    }


}
